<template>
  <div class="goods-comment">
    <!-- <XtxCheckBox v-model="isChecked"/> -->
    <div class="head">
      <div class="data">
        <p>
          <span>{{goodsCount.salesCount}}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{goodsCount.praisePercent}}</span
          ><span>好评率</span>
        </p>
        <p>
          <span>{{goodsCount.evaluateCount}}</span
          ><span>条评价</span>
        </p>
        <p>
          <span>{{goodsCount.hasPictureCount}}</span>
          <span>条有图评价</span>
        </p>
      </div>
    </div>
    <!-- 排序 -->
<div class="sort">
  <span>排序：</span>
  <a
    href="javascript:;"
    :class="{ active: reqParams.sortField === null }"
    @click="changeSort(null)"
    >默认</a
  >
  <a
    href="javascript:;"
    :class="{ active: reqParams.sortField === 'praiseCount' }"
    @click="changeSort('praiseCount')"
    >最热</a
  >
  <a
    href="javascript:;"
    :class="{ active: reqParams.sortField === 'createTime' }"
    @click="changeSort('createTime')"
    >最新</a
  >
 <XtxCheckBox style="margin-left: 50px" v-model="reqParams.hasPicture">是否有图</XtxCheckBox>
</div>
<!-- 列表 -->
<div class="list">
  <div class="item" v-for="item in goodsList" :key="item.id">
    <div class="user">
      <img :src="item.member.avatar" alt="">
      <span>{{ item.member.nickname }}</span>
    </div>
    <div class="body">
      <div class="score">
        <i class="iconfont icon-wjx01" v-for="(score,i) in item.score" :key="i"></i>
        <i class="iconfont icon-wjx02" v-for="(score,i) in 5-item.score" :key="i"></i>
        <span class="attr">颜色：黑色 尺码：M</span>
      </div>
      <div class="text">{{item.content}}</div>
      <!-- 使用图片预览组件 -->
      <GoodsCommentImage :pictures="item.pictures"/>
      <div class="time">
        <span>{{item.createTime}}</span>
        <span class="zan"><i class="iconfont icon-dianzan"></i>{{item.praiseCount}}</span>
      </div>
    </div>
  </div>
</div>
  </div>
</template>
<script>
import { findCommentInfoByGoods, findCommentListByGoods } from '@/api/goods'
import { onMounted, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import GoodsCommentImage from './goods-comment-image.vue'
// 获取统计信息
const useTotal = () => {
  //
  const goodsCount = ref({})
  const route = useRoute()

  const getGoodsCount = async (id) => {
    const res = await findCommentInfoByGoods(id)
    // console.log(res)
    goodsCount.value = res.result
  }

  onMounted(() => {
    getGoodsCount(route.params.id)
  })

  return { goodsCount }
}

// 获取评论信息
const useList = () => {
  const goodsList = ref([])
  const route = useRoute()
  const reqParams = ref({
    page: 1,
    pageSize: 10,
    hasPicture: false, // 是否有
    sortField: null // 排序: praiseCount -> 最热 createTime -> 最新
  })

  // 获取商品评价信息
  const getGoodsList = async () => {
    const res = await findCommentListByGoods(route.params.id, reqParams.value)
    console.log(res)
    goodsList.value = res.result.items
  }

  // 排序
  const changeSort = (sort) => {
    reqParams.value.sortField = sort
    // 重新发送请求
    getGoodsList()
  }

  // 是否有图
  watch(() => reqParams.value.hasPicture, () => {
    // console.log(reqParams)
    // 重新发送请求
    getGoodsList()
  })

  onMounted(() => {
    getGoodsList()
  })

  return { goodsList, reqParams, changeSort }
}
export default {
  name: 'GoodsComment',
  components: {
    GoodsCommentImage
  },
  setup () {
    // const isChecked = ref(false)
    // return { isChecked }
    const sortList = ref([
      { sort: null, name: '默认' },
      { sort: 'praiseCount', name: '最热' },
      { sort: 'createTime', name: '最新' }
    ])
    const { goodsCount } = useTotal()

    const { goodsList, reqParams, changeSort } = useList()

    return { goodsCount, sortList, goodsList, reqParams, changeSort }
  }
}
</script>
<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      flex:1;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
.list {
  padding: 0 20px;
  .item {
    display: flex;
    padding: 25px 10px;
    border-bottom: 1px solid #f5f5f5;
    .user {
      width: 160px;
      img {
        width: 40px;
        height: 40px;
        border-radius: 50%;
        overflow: hidden;
      }
      span {
        padding-left: 10px;
        color: #666;
      }
    }
    .body {
      flex: 1;
      .score {
        line-height: 40px;
        .iconfont {
          color: #ff9240;
          padding-right: 3px;
        }
        .attr {
          padding-left: 10px;
          color: #666;
        }
      }
    }
    .text {
      color: #666;
      line-height: 24px;
    }
    .time {
      color: #999;
      display: flex;
      justify-content: space-between;
      margin-top: 5px;
    }
  }
}
</style>
